package com.koron.car.web.lend.service.impl;

import com.koron.car.web.lend.bean.entity.CarLendItem;
import com.koron.car.web.lend.mapper.CarLendItemMapper;
import com.koron.car.web.lend.service.CarLendItemService;
import com.koron.common.core.util.CodeTools;
import org.apache.commons.collections4.CollectionUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (CarLendItem)表服务实现类
 *
 * @author twcao
 * @since 2022-03-10 16:27:51
 */
@Service
public class LendItemServiceImpl implements CarLendItemService {

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public CarLendItem queryById(SessionFactory factory, String id) {
        CarLendItemMapper mapper = factory.getMapper(CarLendItemMapper.class);
        return mapper.queryById(id);
    }

    /**
     * 查询多条数据
     * @param item 查询条件
     * @return 对象列表
     */
    @Override
    public List<CarLendItem> queryList(SessionFactory factory, CarLendItem item) {
        CarLendItemMapper mapper = factory.getMapper(CarLendItemMapper.class);
        return mapper.queryList(item);
    }

    /**
     * 新增数据
     * @param item 实例对象
     * @return 实例对象
     */
    @Override
    public CarLendItem insert(SessionFactory factory, CarLendItem item) {
        CarLendItemMapper mapper = factory.getMapper(CarLendItemMapper.class);
        item.setId(CodeTools.getCode32());
        mapper.insert(item);
        return item;
    }

    /**
     * 新增数据
     * @param factory
     * @param items   实例对象
     * @return 实例对象
     */
    @Override
    public int insertBatch(SessionFactory factory, List<CarLendItem> items) {
        if(CollectionUtils.isEmpty(items)) {
            return 0;
        }
        CarLendItemMapper mapper = factory.getMapper(CarLendItemMapper.class);
        items.stream().forEach(row -> row.setId(CodeTools.getCode32()));
        mapper.insertBatch(items);
        return items.size();
    }

    /**
     * 修改数据
     * @param item 实例对象
     * @return 实例对象
     */
    @Override
    public CarLendItem update(SessionFactory factory, CarLendItem item) {
        CarLendItemMapper mapper = factory.getMapper(CarLendItemMapper.class);
        return mapper.queryById(item.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(SessionFactory factory, String id) {
        CarLendItemMapper mapper = factory.getMapper(CarLendItemMapper.class);
        return mapper.deleteById(id) > 0;
    }

    @Override
    public Boolean deleteByLendId(SessionFactory factory, String lendId) {
        CarLendItemMapper mapper = factory.getMapper(CarLendItemMapper.class);
        return mapper.deleteByLendId(lendId) > 0;
    }
}
